package com.mud.dnd.ejb;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionManagement;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.domain.dao.DnDDAO;
import com.mud.dnd.domain.model.GameItem;
import com.mud.dnd.ejb.common.AuthenticationException;
import com.mud.dnd.ejb.services.AuthenticationService;

/**
 * Session Bean implementation class CommentManager
 */
@Stateless
@LocalBean
@TransactionManagement
public class PictureManager {

    @EJB
	private DnDDAO dao;
    
    
    @EJB
    private AuthenticationService userService;
    
    private static String picPath = "../pictures";
    
    
    @TransactionAttribute
    public void addPicture(String picturePath, byte[] content,  int gameItemID, String userName, String sessionID) throws DAOException, AuthenticationException, IOException
    {
    	if(!userService.checkSessionForUser(userName, sessionID))
		{
			throw new AuthenticationException("Login required.");
		}
    	if(!userService.checkIfUserIsOwner(userName, gameItemID))
		{
			throw new AuthenticationException("You can edit only your card ir deck.");
			
		}
    	
    	picturePath = (getPicFolder())+picturePath;
    	
		writeFile(content,picturePath);

    	dao.updateGameItem(gameItemID, null, null, "/rest/GameItem/Picture/getPic?gameItemID="+gameItemID, picturePath, 0);
    		
    	
    }
    
    @TransactionAttribute
    public String getPicture(int gameItemID) throws DAOException
    {
    	GameItem item = dao.getGameItembyID(gameItemID);
    	return item.getPicturePath();
    }
    
	@TransactionAttribute
	public File getAllPicture() throws IOException {

		ZipOutputStream zos = null;

		try {

			File picFolder = new File(getPicFolder());
			FileOutputStream fos = new FileOutputStream(getPicFolder()
					+ "/zip/allPic.zip");
			zos = new ZipOutputStream(fos);

			byte[] buffer = new byte[1024 * 1024];

			for (File pic : picFolder.listFiles()) {
				if (!pic.isDirectory()) {

					ZipEntry ze = new ZipEntry(pic.getName());
					zos.putNextEntry(ze);
					FileInputStream in = new FileInputStream(pic);

					int len;
					while ((len = in.read(buffer)) > 0) {
						zos.write(buffer, 0, len);
					}

					in.close();
					zos.closeEntry();
				}

			}
		} catch (IOException e) {
			throw e;
		} finally {

			if (zos != null) {
				zos.close();
			}
		}
  
    	
    	
    	return new File(getPicFolder()+"/zip/allPic.zip");
    }
    

	//save to somewhere
	private void writeFile(byte[] content, String filename) throws IOException {
 
		File file = new File(filename);
 
		if (!file.exists()) {
			file.createNewFile();
		}
 
		FileOutputStream fop = new FileOutputStream(file);
 
		fop.write(content);
		fop.flush();
		fop.close();
 
	}
	
	private String getPicFolder()
	{
		
		return System.getenv("OPENSHIFT_DATA_DIR")!=null ?System.getenv("OPENSHIFT_DATA_DIR")+"/pic/" : picPath;
	}
	
	
    
    

}
